What is filename-regex?
The filename-regex npm package is used to match the filename part of a file path. It provides a simple regular expression for extracting filenames from paths, which can be useful in various file manipulation and validation tasks.
What are filename-regex's main functionalities?
Extract Filename from Path
This feature allows you to extract the filename from a given file path using a regular expression.
const filenameRegex = require('filename-regex');
const path = '/path/to/file.txt';
const match = path.match(filenameRegex());
console.log(match[0]); // Output: 'file.txt'
Validate Filename
This feature allows you to validate if a given string is a valid filename using the regular expression provided by the package.
const filenameRegex = require('filename-regex');
const filename = 'file.txt';
const isValid = filenameRegex().test(filename);
console.log(isValid); // Output: true
Other packages similar to filename-regex
path
The 'path' module is a built-in Node.js module that provides utilities for working with file and directory paths. It includes methods like path.basename() to extract the filename from a path. Unlike filename-regex, it does not use regular expressions but provides a more comprehensive set of path manipulation utilities.
glob
The 'glob' package is used for matching files using patterns. It provides a more powerful and flexible way to find files based on patterns, including filenames. While filename-regex focuses on extracting filenames using a regex, glob can be used to search for files matching complex patterns in directories.
micromatch
The 'micromatch' package is a powerful globbing library that supports advanced matching features. It can be used to match filenames and paths using glob patterns. Compared to filename-regex, micromatch offers more advanced pattern matching capabilities and is suitable for more complex use cases.
filename-regex
Regular expression for matching file names, with or without extension.
Install with npm
npm i filename-regex --save
Usage
The regex is returned as a function.
var regex = require('filename-regex');
var match = 'abc/xyz.md'.match(regex());
var match = 'abc/xyz'.match(regex());
var match = 'abc/.gitignore'.match(regex());
Run tests
Install dev dependencies:
node i -d && mocha
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue
Author
Jon Schlinkert
License
Copyright (c) 2014 Jon Schlinkert
Released under the MIT license
This file was generated by verb on December 28, 2014.